package com.agileboot.attendance.domain.settlement;

import com.agileboot.attendance.domain.settlement.model.SettlementModel;
import com.agileboot.attendance.domain.settlement.model.SettlementModelFactory;
import com.agileboot.attendance.domain.settlement.dto.SettlementQuery;
import com.agileboot.attendance.domain.settlement.dto.SettlementRequest;
import com.agileboot.common.core.page.PageDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 结算单应用服务
 */
@Service
@RequiredArgsConstructor
public class SettlementApplicationService {
    
    private final SettlementModelFactory settlementModelFactory;

    /**
     * 创建结算单
     */
    @Transactional(rollbackFor = Exception.class)
    public void createSettlement(SettlementRequest request) {
        SettlementModel model = settlementModelFactory.create();
        model.loadFromRequest(request);
        model.checkFields();
        model.insert();
    }

    /**
     * 更新结算单
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateSettlement(Long settlementId, SettlementRequest request) {
        SettlementModel model = settlementModelFactory.loadById(settlementId);
        model.loadFromRequest(request);
        model.checkFields();
        model.update();
    }

    /**
     * 删除结算单
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteSettlement(Long settlementId) {
        SettlementModel model = settlementModelFactory.loadById(settlementId);
        model.delete();
    }

    /**
     * 分页查询结算单
     */
    public PageDTO<SettlementModel> getSettlementPage(SettlementQuery query) {
        return settlementModelFactory.selectPage(query);
    }

    /**
     * 获取结算单详情
     */
    public SettlementModel getSettlementInfo(Long settlementId) {
        return settlementModelFactory.loadById(settlementId);
    }
} 